//
//  CAIPhotoBrowser.m
//  CAIImagePickerControllerKit
//
//  Created by 海参 on 2022/8/9.
//

#import "CAIPhotoBrowser.h"
#import "CAIPhotoBrowseCollectionCell.h"

@interface CAIPhotoBrowser ()

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UICollectionViewFlowLayout *collectionFlowLayout;

//@property (nonatomic, strong) NSArray *imagePathsGroup;


@property (nonatomic, strong) UIPageControl *pageControl;

@end



@implementation CAIPhotoBrowser



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self commonInit];
        [self initUI];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self commonInit];
        [self initUI];
    }
    return self;
    
}


- (void)commonInit
{
    self.infiniteLoop = YES;
    self.autoScroll = NO;
}


- (void)initUI{
    
    [self addSubview:self.collectionView];
    [self addSubview:self.pageControl];
}



#pragma mark - UI

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.collectionFlowLayout];
        collection.backgroundColor = [UIColor whiteColor];
        collection.delegate = self;
        collection.dataSource = self;
        
        [collection registerClass:[CAIPhotoBrowseCollectionCell class] forCellWithReuseIdentifier:@"Cell"];
        collection.scrollEnabled = YES;
        collection.showsVerticalScrollIndicator = NO;
        collection.showsHorizontalScrollIndicator = NO;
        collection.pagingEnabled = YES;
        collection.directionalLockEnabled = YES;
        _collectionView = collection;
    }
    return _collectionView;
}


- (UICollectionViewFlowLayout *)collectionFlowLayout{
    if (!_collectionFlowLayout) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 0;
        _collectionFlowLayout = flowLayout;
    }
    return _collectionFlowLayout;
}





/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */





- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    CAIPhotoBrowseCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    NSInteger index = [self pageControlIndexWithCurrentCellIndex:indexPath.item];
    id photo = [self.images objectAtIndex:index];
    [cell setData:photo];
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self totalCount];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(photoBrowser:didSelectItemAtIndex:)])
    {
        NSInteger index = [self pageControlIndexWithCurrentCellIndex:indexPath.item];
        [self.delegate photoBrowser:self didSelectItemAtIndex:index];
    }
}

/// 移除
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arr = [collectionView indexPathsForVisibleItems];
}

/// 添加
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{

    if ([self.delegate respondsToSelector:@selector(photoBrowser:willDisplayCellForItemAtIndex:)])
    {
        NSInteger index = [self pageControlIndexWithCurrentCellIndex:indexPath.item];
        [self.delegate photoBrowser:self willDisplayCellForItemAtIndex:index];
    }
}

#pragma mark - Setter

- (void)setImages:(NSArray *)images
{
    _images = images;
    if (images.count > 1){
        self.collectionView.scrollEnabled = YES;
    }
    else{
        self.collectionView.scrollEnabled = NO;
    }
    [self.collectionView reloadData];
    [self scrollToCenterIndex];
}


//- (void)setImagePathsGroup:(NSArray *)imagePathsGroup
//{
//    _imagePathsGroup = imagePathsGroup;
//
//    if (imagePathsGroup.count > 1){
//        self.collectionView.scrollEnabled = YES;
//    }
//    else{
//        self.collectionView.scrollEnabled = NO;
//    }
//    self.pageControl.currentPage = 0;
//    [self.collectionView reloadData];
//}



#pragma mark - life circles

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionFlowLayout.itemSize = self.frame.size;
    self.collectionView.frame = self.bounds;
}



#pragma mark - 计算

- (NSInteger)pageControlIndexWithCurrentCellIndex:(NSInteger)index
{
    return (NSInteger)index % self.images.count;
}


- (NSInteger)totalCount{
    NSInteger num = self.infiniteLoop ? self.images.count * 100 : self.images.count;
    return num;
}


- (void)scrollToCenterIndex{
    
    if (self.collectionView.contentOffset.x ==0 && [self totalCount]>0)
    {
        self.currentImageIndex = 0;
    }
}


- (void)setCurrentImageIndex:(NSInteger)currentImageIndex{
    _currentImageIndex = currentImageIndex;
    
    self.pageControl.currentPage = currentImageIndex;
    
    int targetIndex = 0;
    if (self.infiniteLoop) {
        targetIndex = [self totalCount] * 0.5;
    }else{
        targetIndex = 0;
    }
    NSInteger next = targetIndex  + currentImageIndex;
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:next inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}


@end
